package frame.userlogin;

import com.alibaba.druid.pool.DruidDataSource;
import frame.managermeau.ManagerHomepageFrame;
import frame.model.SnowflakeIdWorker;
import frame.model.Utils;
import frame.usermeau.UserHomepageFrame;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class RegisterFrame extends JFrame {

    DruidDataSource dataSource;

    public RegisterFrame(DruidDataSource dataSource) {
        this.setTitle("新用户注册");
        //操作
        this.setContentPane(getPanel());
        //设置窗口
        this.setBounds(580,280,400,350);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }


    /**
     * 基本UI，无需修改
     * @return
     */
    private JPanel getPanel() {

        JPanel jPanel  = new JPanel(null);
        int x = 30,xText = 130, y = 20;
        int width = 100, height = 20;

        //添加组件
        JLabel jLabelName = new JLabel("姓名*");
        jLabelName.setLocation(x,y);
        jLabelName.setSize(width,height);
        jPanel.add(jLabelName);
        JTextArea jTextAreaName = new JTextArea(1,20);
        jTextAreaName.setLocation(xText,y);
        jTextAreaName.setSize(200,20);
        jPanel.add(jTextAreaName);
        y += 40;

        JLabel jLabelPhone = new JLabel("电话*");
        jLabelPhone.setLocation(x,y);
        jLabelPhone.setSize(width,height);
        jPanel.add(jLabelPhone);
        JTextArea jTextAreaPhone = new JTextArea(1,20);
        jTextAreaPhone.setLocation(xText,y);
        jTextAreaPhone.setSize(200,20);
        jPanel.add(jTextAreaPhone);
        y += 40;

        JLabel jLabelIdCard = new JLabel("身份证*");
        jLabelIdCard.setLocation(x,y);
        jLabelIdCard.setSize(width,height);
        jPanel.add(jLabelIdCard);
        JTextArea jTextAreaIdCard = new JTextArea(1,20);
        jTextAreaIdCard.setLocation(xText,y);
        jTextAreaIdCard.setSize(200,20);
        jPanel.add(jTextAreaIdCard);
        y += 40;

        JLabel jLabelPasswd = new JLabel("密码*");
        jLabelPasswd.setLocation(x,y);
        jLabelPasswd.setSize(width,height);
        jPanel.add(jLabelPasswd);


        JPasswordField jTextAreaPasswd = new JPasswordField(20);

        jTextAreaPasswd.setLocation(xText,y);
        jTextAreaPasswd.setSize(200,20);
        jPanel.add(jTextAreaPasswd);
        y += 40;

        JLabel jLabelEmail = new JLabel("邮箱");
        jLabelEmail.setLocation(x,y);
        jLabelEmail.setSize(width,height);
        jPanel.add(jLabelEmail);
        JTextArea jTextAreaEmail = new JTextArea(1,20);
        jTextAreaEmail.setLocation(xText,y);
        jTextAreaEmail.setSize(200,20);
        jPanel.add(jTextAreaEmail);
        y += 40;

        //添加按钮与事件
        JButton jb = new JButton("确认");
        jb.setLocation(xText+40,y);
        jb.setSize(60,40);
        jb.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String name = jTextAreaName.getText();
                String phoneNumber = jTextAreaPhone.getText();
                String idCard = jTextAreaIdCard.getText();
                String passwd = jTextAreaPasswd.getText();
                String email = jTextAreaEmail.getText();

                if(name.length() == 0 || phoneNumber.length() == 0 ||  idCard.length() == 0 || passwd.length() == 0) {
                    JOptionPane.showMessageDialog(null, "请输入必填信息！");
                }else if (phoneNumber.length() != 11){
                    JOptionPane.showMessageDialog(null, "电话号码需要11位哦");
                }else if(idCard.length() != 18) {
                    JOptionPane.showMessageDialog(null, "身份证号需要18位哦");
                }else if(Utils.varify("t_user","u_phone",phoneNumber)) {
                    JOptionPane.showMessageDialog(null, "已有用户使用该电话号码");
                }else if(Utils.varify("t_user","u_id_card",idCard)) {
                    JOptionPane.showMessageDialog(null, "你的身份证号已注册");
                }else {
                    if(email.length() == 0) {
                        register(name, phoneNumber, idCard, passwd,"");
                    }
                    else {
                        register(name, phoneNumber, idCard, passwd, email);
                    }
                }
            }
        });
        jPanel.add(jb);
        return jPanel;
    }


    /**
     * 注册（验证，注册成功与否都会给出返回弹窗，注册成功则跳转）
     * @param name
     * @param phoneNumber
     * @param IdCard
     * @param passwd
     * @param email
     */
    private void register(String name, String phoneNumber, String IdCard, String passwd, String email) {



        //返回信息
        String message = "添加成功";
        JOptionPane.showMessageDialog(null, message);

        //跳转用户主界面
        new UserHomepageFrame();
        this.dispose();
    }

    public static void main(String[] args) {
        new RegisterFrame(new DruidDataSource());
    }


}
